LearningStoreTransactionScope Class

Makes the LearningStore-related operations in a code block transactional.

Namespace: Microsoft.LearningComponents.Storage

Assembly: Microsoft.LearningComponents.Storage.dll

Syntax
C#

public sealed class LearningStoreTransactionScope : IDisposable

Constructors
Name Description
Ð LearningStoreTransactionScope ()

Initializes a new instance of the LearningStoreTransactionScope class.

Ð LearningStoreTransactionScope (Transaction)

Initializes a new instance of the LearningStoreTransactionScope class. LearningStore jobs executed within the scope use the specified transaction.

Ð LearningStoreTransactionScope (TransactionOptions)

Initializes a new instance of the LearningStoreTransactionScope class with the specified options.

Properties
Name Description
ÐS Current

Current scope

Methods
Name Description
Ð Complete

Indicates that all operations within the scope have completed successfully.

Ð Dispose

Ends the transaction scope.

Remarks

By default, LearningStoreJob.Execute creates a new transaction every time it is called. If LearningStoreJob.Execute is called on several different jobs, then each job executes within a different transaction. To execute multiple jobs within the same transaction, execute the jobs within a LearningStoreTransactionScope (that is, between the initialization of a LearningStoreTransactionScope object and the calling of its Dispose method). The initialization method will create the transaction, and the Dispose method will commit or rollback the transaction.

When your application completes all work it wants to perform in a scope, you should call the Complete method only once to specify that it is acceptable to commit the transaction. If this method is not called, the transaction is rolled back when the scope exits.

Transaction scopes can be nested. However, nested scopes use the transaction from the containing scope. Any options specified when creating nested scopes must be compatible with the outermost scope. See LearningStoreTransactionScope for more details.

LearningStoreTransactionScope is, in some ways, similar to System.Transactions.TransactionScope. The major differences include:

Example

The following code finds the user with the name "John Doe" and deletes him within a single transaction:

C#
using(LearningStoreTransactionScope scope = new LearningStoreTransactionScope())
{
    // Create the query
    LearningStoreQuery query = store.CreateQuery("UserItem");
 
    // Add output columns to the query
    query.AddColumn("Id");
 
    // Add a condition
    query.AddCondition("Name", LearningStoreConditionOperator.Equal, "John Doe");
 
    // Create a job and add the "perform query" operation
    LearningStoreJob job = store.CreateJob();
    job.PerformQuery(query);
 
    // Execute the job
    DataTable result = job.Execute<DataTable>();
 
    // Get the Id
    LearningStoreItemIdentifier id = (LearningStoreItemIdentifier)(result[0][0]);
 
    // Create a new job and add the "delete" operation
    job = store.CreateJob();
    job.DeleteItem(id);
 
    // Execute the job
    job.Execute();
 
    // Specify that the operations have succeeded
    scope.Complete();
}